<template>
	<div class="zs-table-page-container">
		<div class="zs-card-sm zs-space-bottom zs-layout-flex-row zs-layout-justify-between">
			<span>
				<el-input v-model="projectName" class="zs-select-md zs-space-right-sm" placeholder=" " disabled />
				<el-input v-model="queryParams.biHouseNu" class="zs-select-md zs-space-right-sm" placeholder="房间编号" @blur="queryList" clearable />
				<el-select v-model="queryParams.floorNum" class="zs-select-md zs-space-right-sm" placeholder="楼层" @change="queryList" clearable filterable>
					<el-option v-for="item in floorList" :key="item.floorNum" :label="item.floorNum" :value="item.floorNum" />
				</el-select>
				<el-popover popper-class="zs-popper" placement="bottom-end" trigger="click">
					<template #reference>
						<el-button class="zs-btn-square-solid" :class="{ 'zs-btn-square-solid-active': queryParams.landEncoding || queryParams.effectStatus }">
							<el-icon>
								<MoreFilled />
							</el-icon>
						</el-button>
					</template>
					<template #default>
						<el-select v-model="queryParams.landEncoding" class="zs-select-md zs-space-right-sm" placeholder="楼栋" @change="queryList" clearable filterable :teleported="false">
							<el-option v-for="item in buildingList" :key="item.landEncoding" :label="item.buildingName" :value="item.landEncoding" />
						</el-select>
						<el-select v-model="queryParams.effectStatus" class="zs-select-md zs-space-right-sm" placeholder="生效状态" @change="queryList" clearable :teleported="false">
							<el-option v-for="item in dictObj.VALIDATE_FLAG" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
						</el-select>
					</template>
				</el-popover>
				<el-button class="zs-btn-plain" @click="queryList" v-btnPermi="{ permis: ['p_btn_property_inspection_list_query'] }"> 查询 </el-button>
			</span>
		</div>

		<!-- teble -->
		<div class="zs-card zs-table-page-content">
			<el-table class="zs-table-page-main" :data="tableData.data" v-loading="tableData.loading" stripe>
				<el-table-column type="index" label="序号" width="60" align="center" :index="index => index + 1 + (tableData.currentPage - 1) * curPageSize" />
				<el-table-column prop="biBuildName" label="楼栋" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="floorNum" label="楼层" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="unitNum" label="单元编号" align="left" show-overflow-tooltip :formatter="defaultTableData" />
				<el-table-column label="生效状态" align="center">
					<template #default="{ row }">
						<span :class="row.effectStatus == 1 ? 'zs-validata-status-yes' : 'zs-validata-status-no'">{{ row.effectStatus == 1 ? '有效' : '失效' }}</span>
					</template>
				</el-table-column>
				<el-table-column label="审核状态" align="center">
					<template #default="{ row }">
						<span :class="row.auditStatus == 1 ? 'zs-validata-status-yes' : 'zs-validata-status-no'">{{ row.auditStatus == 1 ? '通过' : '驳回' }}</span>
					</template>
				</el-table-column>
				<el-table-column label="审核时间" align="center" :formatter="defaultTableData">
					<template #default="{ row }">
						{{ row.auditTime ? parseTime(row.auditTime, '{y}-{m}-{d}') : '-' }}
					</template>
				</el-table-column>
				<el-table-column prop="" label="定义" width="90" align="center">
					<template #default="{ row }">
						<el-button class="zs-link" link size="small" @click="openDrawer(false, 'edit', row, 'base')">查看</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="zs-space-top-sm zs-table-page-pagination">
				<el-pagination v-model:currentPage="tableData.currentPage" v-model:page-size="curPageSize" :page-sizes="curPageSizes" :small="true" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="pageChange('size', $event)" @current-change="pageChange('page', $event)" />
			</div>
		</div>
	</div>

	<!-- 定义抽屉 -->
	<drawer-residence-info :isShow="drawer.base.isShowDrawer" :isReadOnly="drawer.base.isReadOnly" :handleType="drawer.base.handleType" :currentRow="currentRow.data" @close="closeDrawer($event, 'base')"> </drawer-residence-info>
</template>

<script setup>
	import { residenceRoomListApi, getBuildingListApi, getFloorListApi } from '@/api/residence.js';
	import drawerResidenceInfo from './components/drawerResidenceInfo.vue';
	import { ref, reactive } from 'vue';
	import { useStore } from 'vuex';
	import { useRoute } from 'vue-router';
	import { defaultTableData, parseTime } from '@/utils/publicUtils';

	const route = useRoute();
	const store = useStore();
	const curPageSizes = ref(store.state.uiPageSizes);
	const curPageSize = ref(store.state.uiPageSizeDefault);
	const dictObj = store.getters.getDictObj(['VALIDATE_FLAG']);
	const projectName = ref(decodeURI(route.query.projectName || ''));

	// 列表 filed
	const tableData = reactive({
		currentPage: 1,
		total: 0,
		loading: false,
		data: [],
	});

	//查询条件
	const queryParams = reactive({ projectCode: route.query.projectCode, pageNumber: 1, pageSize: curPageSize.value });

	// 抽屉相关
	const currentRow = reactive({ data: {} });
	const drawer = reactive({
		base: {
			handleType: '',
			isReadOnly: false,
			isShowDrawer: false,
		},
	});

	const openDrawer = (flag, handleType, row, drawerType) => {
		drawer[drawerType].isReadOnly = flag;
		drawer[drawerType].isShowDrawer = true;
		drawer[drawerType].handleType = handleType;
		currentRow.data = row;
	};

	const closeDrawer = (data, drawerType) => {
		drawer[drawerType].isShowDrawer = data.isShow;
		data.isRefresh && queryList('init');
	};

	// 列表数据源
	const queryList = async init => {
		if (process.env.NODE_ENV !== 'development') {
			let portalResourceList = JSON.parse(localStorage.getItem('portalResourceList'));
			if (init != 'init' && !portalResourceList.includes('p_btn_property_inspection_list_query')) return;
		}
		tableData.loading = true;
		let res = await residenceRoomListApi(queryParams);
		if (res.data.code !== 200) return;
		tableData.data = res.data.data.list || [];
		tableData.total = res.data.data.total || 0;
		tableData.loading = false;
	};

	//楼栋
	const buildingList = ref([]);
	const getBuildingList = async () => {
		let res = await getBuildingListApi({ projectCodes: [route.query.projectCode], isPage: false, isNew: true });
		if (res.data.code !== 200) return;
		buildingList.value = res.data.data.list || [];
	};

	//楼层
	const floorList = ref([]);
	const getFloorList = async init => {
		let res = await getFloorListApi({ projectCode: route.query.projectCode });
		if (res.data.code !== 200) return;
		floorList.value = res.data.data || [];
	};

	// 分页
	const pageChange = (type, size) => {
		if (type == 'size') {
			queryParams.pageSize = size;
		} else {
			queryParams.pageNumber = size;
		}
		queryList('init');
	};

	getBuildingList();
	getFloorList();
	queryList('init');
</script>
